package com.zhongxi.guoyuan.view;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.widget.view.CustomVideoView;

public class PlayActivity extends BaseActivity implements View.OnClickListener {

    private CustomVideoView videoView;
    private String url = "";
    private ProgressDialog progress;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_play);

        videoView = (CustomVideoView) findViewById(R.id.videoView);
        ivBack = (ImageView) findViewById(R.id.ivBack);

        url = getIntent().getStringExtra("url");

        progress = ProgressDialog.show(this, "loading", "视频播放");
        progress.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_BACK){
                    dialog.dismiss();
                    finish();
                }
                return false;
            }
        });

        videoView.setVideoURI(Uri.parse(url));
        MediaController controller = new MediaController(this);
        videoView.setMediaController(controller);
        videoView.requestFocus();

        videoView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if(progress!=null)
                    progress.dismiss();
            }
        });

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if(progress!=null)
                    progress.dismiss();
            }
        });
        videoView.start();
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }
}
